package com.puxsoft.example.web;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.puxsoft.example.domain.Customer;
import com.puxsoft.example.service.CustomerService;
import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiImplicitParam;
import com.wordnik.swagger.annotations.ApiImplicitParams;
import com.wordnik.swagger.annotations.ApiOperation;
import com.wordnik.swagger.annotations.ApiResponse;
import com.wordnik.swagger.annotations.ApiResponses;

@RestController
@RequestMapping("/customer")
@Api("CustomerController相关api")
public class CustomerController {

    @Autowired
    private CustomerService customerService;
    
    @ApiOperation("添加客戶")
    @ApiImplicitParams({
        @ApiImplicitParam(paramType="query",name="customerName",dataType="String",required=true,value="客戶的姓名",defaultValue="zhaojigang"),
        @ApiImplicitParam(paramType="query",name="phone",dataType="String",required=true,value="客戶的手机",defaultValue="18978909090")
    })
    @ApiResponses({
        @ApiResponse(code=400,message="请求参数没填好"),
        @ApiResponse(code=404,message="请求路径没有或页面跳转路径不对")
    })
    @RequestMapping(value="/addUser",method=RequestMethod.POST)
    public void addUser(@RequestParam("customerName") String customerName, 
                           @RequestParam("phone") String phone) {
        customerService.insertCustomer(customerName, phone);
    }
    
    @ApiOperation("添加用户且返回已经设置了主键的user实例")
    @ApiImplicitParams({
        @ApiImplicitParam(paramType="query",name="customerName",dataType="String",required=true,value="客戶的姓名",defaultValue="zhaojigang"),
        @ApiImplicitParam(paramType="query",name="phone",dataType="String",required=true,value="客戶的手机",defaultValue="18978909090")
    })
    @ApiResponses({
        @ApiResponse(code=400,message="请求参数没填好"),
        @ApiResponse(code=404,message="请求路径没有或页面跳转路径不对")
    })
    @RequestMapping(value="/addUserWithBackId",method=RequestMethod.GET)
    public Customer addUserWithBackId(@RequestParam("customerName") String customerName, 
                                     @RequestParam("phone") String phone) {
        return customerService.insertUserWithOld(customerName, phone);
    }
    
    @RequestMapping(value="/id/{customerId}",method=RequestMethod.GET)
    @ResponseBody
    public Customer findById(@PathVariable String customerId) {
        return customerService.findById(customerId);
    }
    
    @RequestMapping(value="/all",method=RequestMethod.GET)
    @ResponseBody
    public List<Customer> findAll() {
        return customerService.findAll();
    }
}
